{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "AugLy_video.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "1-7Y4fTlOLud"
      },
      "source": [
        "# Note: restart runtime after this import before running the augmentations\n",
        "!pip install -U augly[video]\n",
        "!sudo apt-get install python3-magic"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kOPzDGWBOQCD"
      },
      "source": [
        "# Installing ffmpeg is only needed for the video module of augly\n",
        "!sudo add-apt-repository ppa:jonathonf/ffmpeg-4\n",
        "!apt install ffmpeg"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qqDjJm7l7mCl"
      },
      "source": [
        "from IPython.display import display, HTML\n",
        "from base64 import b64encode\n",
        "\n",
        "def display_video(path):\n",
        "  mp4 = open(path, \"rb\").read()\n",
        "  data_url = \"data:video/mp4;base64,\" + b64encode(mp4).decode()\n",
        "  display(\n",
        "    HTML(\n",
        "      \"\"\"\n",
        "          <video width=400 controls>\n",
        "                <source src=\"%s\" type=\"video/mp4\">\n",
        "          </video>\n",
        "      \"\"\" % data_url\n",
        "    )\n",
        "  )"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WovUbAUtOlHX"
      },
      "source": [
        "import os\n",
        "import augly.utils as utils\n",
        "import augly.video as vidaugs\n",
        "\n",
        "# Get input video, trim to first 3 seconds\n",
        "input_video = os.path.join(\n",
        "    utils.TEST_URI, \"video\", \"inputs\", \"input_1.mp4\"\n",
        ")\n",
        "input_vid_path = \"/tmp/in_video.mp4\"\n",
        "out_vid_path = \"/tmp/aug_video.mp4\"\n",
        "\n",
        "# We can use the AugLy trim augmentation, and save the trimmed video\n",
        "vidaugs.trim(input_video, output_path=input_vid_path, start=0, end=3)\n",
        "display_video(input_vid_path)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OoTw6x6j7n6W"
      },
      "source": [
        "# Now we can apply various augmentations to the trimmed video!\n",
        "vidaugs.overlay_text(input_vid_path, out_vid_path)\n",
        "display_video(out_vid_path)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0xvGrGYd79qU"
      },
      "source": [
        "\"\"\"\n",
        "You can optionally pass in a metadata list, to which metadata about the\n",
        "augmentation will be appended, including kwargs, input & output dimensions,\n",
        "the matching segments in the input & output videos (useful in case of temporal\n",
        "editing), and intensity (defined based on the kwargs for each augmentation).\n",
        "\"\"\"\n",
        "meta = []\n",
        "vidaugs.loop(\n",
        "    input_vid_path,\n",
        "    out_vid_path,\n",
        "    num_loops=1,\n",
        "    metadata=meta,\n",
        ")\n",
        "display_video(out_vid_path)\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3oUFL8Xr-R8k"
      },
      "source": [
        "# For all the augmentations, we have class-based definitions as well as\n",
        "# functional\n",
        "meta = []\n",
        "aug = vidaugs.InsertInBackground()\n",
        "aug(input_vid_path, out_vid_path, metadata=meta)\n",
        "display_video(out_vid_path)\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HwsVVGgD6fKf"
      },
      "source": [
        "\"\"\"\n",
        "For some augmentations, we also provide versions that will randomly sample\n",
        "from a set of parameters (e.g. for OverlayEmoji, RandomEmojiOverlay samples\n",
        "an emoji from Twitter's Twemoji set which we provide in the augly package).\n",
        "The metadata will contain the actual sampled param values.\n",
        "\"\"\"\n",
        "meta = []\n",
        "aug = vidaugs.RandomEmojiOverlay()\n",
        "aug(input_vid_path, out_vid_path, metadata=meta)\n",
        "display_video(out_vid_path)\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dwc1Y_1g6e4A"
      },
      "source": [
        "# You can also compose several transformations together\n",
        "aug = vidaugs.Compose(\n",
        "    [\n",
        "        vidaugs.AddNoise(),\n",
        "        vidaugs.Blur(sigma=5.0),\n",
        "        vidaugs.OverlayDots(),\n",
        "    ]\n",
        ")\n",
        "aug(input_vid_path, out_vid_path)\n",
        "display_video(out_vid_path)"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}